var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()
var Store = app.Store

Page({
  data: {
    form:{}, // redux传过来的参数
    ticketNumber:'',
    isShow: false,  
    titleType:'添加', // 当前选中的select [id] 
    urls:['http://223.244.87.84:9300/profile/lp/gzp.png'],
    reciveTime:'2024-11-01 09:10:11',
    attention:'',
    additional:'',
    pzStartTime:'',
    pzEndTime:'',
    rejectReason:'',
    signImg:'',
    basic:{},
    isLose:'',
    endRemark:'',
    type:'',
    nowDate:'',
  },
  onLoad: function (options) {
    this.setData({type:options.type,ticketNumber:options.ticketNumber,nowDate:$.transformTime()})
    api.$get(api.workTicket.workTickInfo+options.ticketNumber).then((res) => {
      this.setData({
        urls:[res.data.imagePath+`?a=${Math.random()*100}`],
        basic:res.data,
        pzStartTime:res.data.startTime,
        pzEndTime:res.data.endTime,
      })
      wx.previewImage({
        current: '', // 当前显示图片的链接，不填则默认为urls的第一张
        urls: this.data.urls // 需要预览的图片链接列表
      })
    })
    
  },
  onShow: function () {
    var form = Store.getState().formInfor.form
    var key = form.key
    console.log(form)
    switch (key) {
      case 'workSign': // 获取签发人签名  
        this.setData({
          signImg: form.selected,
        })
        break 
      default:
        this.setData({
          signImg: wx.getStorageSync('signUrl'),
        })
        break;
    }
    console.log(this.data.signImg)
  },
showTickets(e) {
  const {type,item,idx}=$.detail(e)
  wx.previewImage({
    current: '', // 当前显示图片的链接，不填则默认为urls的第一张
    urls: this.data.urls // 需要预览的图片链接列表
  })
  
},  
beginSign(e){ 
  var that=this
  console.log(that.data.signImg)
  if(!Boolean(that.data.signImg)){
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'workSign', 
      },
    })
    $.linkurl(`/pages/components/signatrue/signAture?ticketNumber=${that.data.ticketNumber}`) 
  }
    // wx.showModal({
    //   title: '提示',
    //   content: '使用原有签名或者手动签名',
    //   confirmText:'原有签名',
    //   cancelText:'手动签名',
    //   success (res) {
    //     if (res.confirm) {
          
    //       that.setData({
    //         signImg: wx.getStorageSync('signUrl'),
    //       })
    //     } else if (res.cancel) {
    //       Store.dispatch({
    //         type: 'SET_FROM',
    //         form: {
    //           key: 'workSign', 
    //         },
    //       })
    //       $.linkurl(`/pages/components/signatrue/signAture?ticketNumber=${that.data.ticketNumber}`) 
    //     }
    //   }
    // })
 
},
bigSign(e){
  const {img}=$.detail(e)
  wx.previewImage({
    current: '', // 当前显示图片的链接，不填则默认为urls的第一张
    urls: [img] // 需要预览的图片链接列表
  })
},
input_handle(e) { 
  this.setData({
    [e.currentTarget.dataset.key]: e.detail.value
  })
},
mattersAttention(e){
  console.log(e.detail.value)
  this.setData({'attention':e.detail.value})
},
additionalMeasures(e){
  this.setData({'additional':e.detail.value})
},
openTimeHandle(e) { // 打开时间弹框
  const {item}=$.detail(e)
  if(item=='pzStartTime'){
    this.setData({
      isShowTime: true
    })
  }else if(item=='pzEndTime'&&this.data.pzEndTime!=''){
    this.setData({
      isShowTime2: true
    })
  }else{
    $.toast("请先选择开始时间")
  }
},
timeConfirmHandle(e) { // 时间组件传出来的数据
  var { time, key } = e.detail

  this.setData({
    [key]: time
  })

  console.log(time, key)
},
openLoseHandle() { // 驳回弹窗
  this.setData({ isLose: true })
},
//签发人审核
issuerExame(approvalResult,approvalRemark){
  if(!Boolean(this.data.signImg)){
    return $.toast('当前无默认签名，请手动填写！')
  }
  api.$get(api.workTicket.issuerExame,{
    ticketNumber:this.data.ticketNumber,
    signature:this.data.signImg,
    approvalResult:approvalResult,
    approvalRemark:approvalRemark,
    supplementarySafetyMeasures:this.data.attention
  }).then((res) => {
    if(res.code==200){
      $.linkurl(``,'back')
    }
  })
},
//外部签发人审核
externalIssuerExame(approvalResult,approvalRemark){
  if(!Boolean(this.data.signImg)){
    return $.toast('当前无默认签名，请手动填写！')
  }
  api.$get(api.workTicket.externalIssuerExame,{
    ticketNumber:this.data.ticketNumber,
    signature:this.data.signImg,
    approvalResult:approvalResult,
    approvalRemark:approvalRemark
  }).then((res) => {
    if(res.code==200){
      $.linkurl(``,'back')
    }
  })
},
//中心值班人审核
dutyCenterExame(approvalResult,approvalRemark){
  if(!Boolean(this.data.signImg)){
    return $.toast('当前无默认签名，请手动填写！')
  }
  api.$get(api.workTicket.dutyCenterExame,{
    ticketNumber:this.data.ticketNumber,
    signature:this.data.signImg,
    approvalResult:approvalResult,
    approvalRemark:approvalRemark
  }).then((res) => {
    if(res.code==200){
      $.linkurl(``,'back')
    }
  })
},
//值班人批准时间
permiterExame(approvalResult,approvalRemark){
  if(this.data.pzStartTime==''||this.data.pzEndTime==''){
    $.toast('请选择开始时间和结束时间')
    return false
  }
  if(!Boolean(this.data.signImg)){
    return $.toast('当前无默认签名，请手动填写！')
  }
  api.$get(api.workTicket.permiterExame,{
    ticketNumber:this.data.ticketNumber,
    signature:this.data.signImg,
    approvalResult:approvalResult,
    approvalRemark:approvalRemark,
    startTime:this.data.pzStartTime,
    endTime:this.data.pzEndTime,
    supplementarySafetyMeasures:this.data.additional
  }).then((res) => {
    if(res.code==200){
      $.linkurl(``,'back')
    }
  })
},
//许可人许可
permitIssuerMeasuresConfirm(){
  if(this.data.pzStartTime==''){
    $.toast('请选择许可开始时间')
    return false
  }
  if(!Boolean(this.data.signImg)){
    return $.toast('当前无默认签名，请手动填写！')
  }
  api.$get(api.workTicket.permitIssuerMeasuresConfirm,{
    ticketNumber:this.data.ticketNumber,
    signature:this.data.signImg,
    startTime:this.data.pzStartTime,
  }).then((res) => {
    if(res.code==200){
      $.linkurl(``,'back')
    }
  })
},
//工作班成员确认安全措施
teamMemberMeasuresConfirm(approvalResult,approvalRemark){
  if(!Boolean(this.data.signImg)){
    return $.toast('当前无默认签名，请手动填写！')
  }
  api.$get(api.workTicket.teamMemberMeasuresConfirm,{
    ticketNumber:this.data.ticketNumber,
    signature:this.data.signImg,
    approvalResult:approvalResult,
    approvalRemark:approvalRemark
  }).then((res) => {
    if(res.code==200){
      $.linkurl(``,'back')
    }
  })
},
//工作班成员审核风控卡
riskMemberConfirm(approvalResult,approvalRemark){
  if(!Boolean(this.data.signImg)){
    return $.toast('当前无默认签名，请手动填写！')
  }
  api.$get(api.workTicket.riskMemberConfirm,{
    ticketNumber:this.data.ticketNumber,
    signature:this.data.signImg,
    approvalResult:approvalResult,
    approvalRemark:approvalRemark
  }).then((res) => {
    if(res.code==200){
      $.linkurl(``,'back')
    }
  })
},
//确认开始工作
workStartWorkLeaderConfirm(){
  if(!Boolean(this.data.signImg)){
    return $.toast('当前无默认签名，请手动填写！')
  }
  api.$get(api.workTicket.workStartWorkLeaderConfirm,{
    ticketNumber:this.data.ticketNumber,
    signature:this.data.signImg,
  }).then((res) => {
    if(res.code==200){
      $.linkurl(``,'back')
    }
  })
},
//工作负责人工作票终结
workEndWorkLeaderConfirm(approvalResult,approvalRemark){
  if(!Boolean(this.data.signImg)){
    return $.toast('当前无默认签名，请手动填写！')
  }
  api.$get(api.workTicket.workEndRemark,{
    ticketNumber:this.data.ticketNumber,
    endRemark:this.data.endRemark,
  }).then((res) => {
    if(res.code==200){
      api.$get(api.workTicket.workEndWorkLeaderConfirm,{
        ticketNumber:this.data.ticketNumber,
        signature:this.data.signImg,
        approvalResult:approvalResult,
        approvalRemark:approvalRemark
      }).then((res) => {
        if(res.code==200){
          $.linkurl(``,'back')
        }
      })
    }
  })
},
//许可人终结工作票
workEndWorkPermitIssuerConfirm(approvalResult,approvalRemark){
  if(!Boolean(this.data.signImg)){
    return $.toast('当前无默认签名，请手动填写！')
  }
  api.$get(api.workTicket.workEndWorkPermitIssuerConfirm,{
    ticketNumber:this.data.ticketNumber,
    signature:this.data.signImg,
    approvalResult:approvalResult,
    approvalRemark:approvalRemark
  }).then((res) => {
    if(res.code==200){
      $.linkurl(``,'back')
    }
  })
},
setReject(){//驳回
  if($.isNull(this.data.rejectReason)){
    $.toast("驳回原因不能为空")
      return
  }
  if(this.data.type=='签发人签发')this.issuerExame(true,this.data.rejectReason)
  if(this.data.type=='外部签发人签发')this.externalIssuerExame(true,this.data.rejectReason)
  if(this.data.type=='值班人接收')this.dutyCenterExame(true,this.data.rejectReason)
  if(this.data.type=='值班人批准')this.permiterExame(true,this.data.rejectReason)
  if(this.data.type=='班组人员审核')this.riskMemberConfirm(true,this.data.rejectReason)
  if(this.data.type=='负责人终结')this.workEndWorkLeaderConfirm(true,this.data.rejectReason)
  if(this.data.type=='许可人终结')this.workEndWorkPermitIssuerConfirm(true,this.data.rejectReason)
},
cancelBack(e){
  $.linkurl('', 'back')
},
updateHandle(){
  if(this.data.type=='签发人签发')this.issuerExame(true,'')
  if(this.data.type=='外部签发人签发')this.externalIssuerExame(true,'')
  if(this.data.type=='值班人接收')this.dutyCenterExame(true,'')
  if(this.data.type=='值班人批准')this.permiterExame(true,'')
  if(this.data.type=='许可人许可')this.permitIssuerMeasuresConfirm()
  if(this.data.type=='班组人员审核')this.riskMemberConfirm(true,'')
  if(this.data.type=='负责人终结')this.workEndWorkLeaderConfirm(true,'')
  if(this.data.type=='许可人终结')this.workEndWorkPermitIssuerConfirm(true,'')
  if(this.data.type=='班组成员安全措施确认')this.teamMemberMeasuresConfirm(true,'')
  if(this.data.type=='确认开始工作')this.workStartWorkLeaderConfirm()
}
})